<template>
  <div class="score">
    <div class="header">
      <el-select
        v-model="selectYear"
        placeholder="选择年份"
        @change="yearChange"
      >
        <el-option
          :label="ele"
          :value="ele"
          :key="ele"
          v-for="ele in yearOptions"
        ></el-option>
      </el-select>
    </div>
    <div class="table">
      <el-card shadow="hover">
        <template #header>
          <div class="card-header">
            <span>录取分析</span>
          </div>
        </template>
        <div class="mohu" style="padding: 20px">
          <el-table
            v-loading="loading"
            stripe
            element-loading-text="数据加载中..."
            empty-text="暂无数据，请重新选择条件查询"
            :data="tableData"
            style="width: 100%"
          >
            <el-table-column prop="lqzdf" label="最低分（排除专项计划）" />
            <el-table-column prop="lqzgf" label="最高分" />
            <el-table-column prop="jyfs" label="建议分" />
            <el-table-column fixed="right" label="操作" width="60">
              <template #default="scope">
                <el-button
                  link
                  type="primary"
                  size="small"
                  @click="handleClick(scope.row)"
                  >编辑</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
    </div>
    <div class="con">
      <div
        v-loading="loading"
        element-loading-text="数据加载中..."
        style="margin-top: 30px"
      >
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>复试分析</span>
            </div>
          </template>
          <div class="mohu" style="padding: 20px">
            <div class="text">
              <!-- 进复试人数2人，拟录取2人， -->
							<div class="proportion" style="display: flex;justify-content: flex-start;align-items: center;">
                <div>
                  复录比为
                </div>
                <div style="margin-left: 10px;">
                  <el-input v-model="CardFormData.fuluRaio" placeholder="复录比为"/>
                </div>
                <el-icon  style="margin-left: 10px;" class="proportion-icon" size="22px" @click="onProportion" color="#F56C6C"><QuestionFilled /></el-icon>
              </div>
              <br/>
              <div style="display: flex;justify-content: flex-start;align-items: center;">
                <div>
                  进复试最低分
                </div>
                <el-input style="width: 200px;margin-left: 10px;" v-model="CardFormData.fuLowScore" placeholder="进复试最低分"/>
                <div style="margin-left: 10px;">
                  进复试最高分
                </div> 
                <el-input style="width: 200px;margin-left: 10px;" v-model="CardFormData.fuHighScore" placeholder="进复试最高分"/>
                <div style="margin-left: 10px;">
                  进复试中位分
                </div> 
                <el-input style="width: 200px;margin-left: 10px;" v-model="CardFormData.fuMedianScore" placeholder="进复试中位分"/>
              </div>
						</div>
            <div style="display: flex; justify-content: center; margin-top: 20px">
              <el-button type="primary" @click="confirmEdit(2)">确定修改</el-button>
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <el-dialog
      v-model="editDialog"
      title="编辑录取分析"
      width="500px"
      :before-close="handleClose"
    >
      <div class="upload">
        <el-form
          ref="formRef"
          :model="formData"
          label-width="100px"
          class="demo-ruleForm"
          status-icon
        >
          <el-form-item label="最低分" prop="lqzdf">
            <el-input v-model="formData.lqzdf" />
          </el-form-item>
          <el-form-item label="最高分" prop="lqzgf">
            <el-input v-model="formData.lqzgf" />
          </el-form-item>
          <el-form-item label="建议分" prop="jyfs">
            <el-input v-model="formData.jyfs" />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialog = false">取消</el-button>
          <el-button type="primary" @click="confirmEdit(1)">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { onMounted, defineProps, ref } from "vue";
import { scoreAnalysis, correctMajorInfo, fuluStudentAnalysis } from "@/api/api";
import { decrypt } from "@/utils/parseDate";
import { defineEmits } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import "element-plus/theme-chalk/el-message-box.css";

const props = defineProps({
  majorId: String,
});

// 数据加载中
const loading = ref(false);
const selectYear = ref("");
const yearOptions = ref([]);
const formDataList = ref([]);
const CardFormData = ref({});
const tableData = ref([]);
// 所有数据
const allData = ref([]);
// 将 myMethod 暴露给父组件
defineExpose({ childMethod });
function childMethod() {
  getScoreAnalysis();
}
onMounted(() => {
  getScoreAnalysis();
  getFuluStudentAnalysis();
});
function onProportion() {
  ElMessageBox.confirm("复录比=已知复试人数/录取名单人数。部分学校的调剂录取名单可能没有统计在复试名单中，所以部分学校复录比可能小于1", {
    confirmButtonText: "确定",
    title: "提示",
    type: "warning",
    center: true,
    closeOnClickModal: false,
  })
    .then(() => {
      
    })
    .catch(() => {
      
    });
}
async function getFuluStudentAnalysis() {
  try {
    loading.value = true;
    const data = {
      majorOutRelationId: props.majorId,
    };
    const result = await fuluStudentAnalysis(data);
    const res = JSON.parse(decrypt(result));
    console.log(res,9);
    if (res.status === 0 && res.result.length > 0) {
      formDataList.value = res.result;
      CardFormData.value = res.result.filter((ele) => ele.year == selectYear.value)[0];
      loading.value = false;
    } else {
      
    }
  } catch (error) {
    // throw new Error(error)
  }
}
// 获取分数分析数据
async function getScoreAnalysis() {
  try {
    loading.value = true;
    const data = {
      majorOutRelationId: props.majorId,
    };
    const result = await scoreAnalysis(data);
    const res = JSON.parse(decrypt(result));
    console.log(res);
    if (res.status === 0 && res.result.length > 0) {
      const yearList = [...new Set(res.result.map((ele) => ele.ssnf))];
      yearOptions.value = yearList.reverse();
      allData.value = res.result;
      selectYear.value = yearList[0];
      tableData.value = res.result.filter((ele) => ele.ssnf == yearList[0]);
      loading.value = false;
    } else {
      tableData = [];
      allData = [];
      selectYear.value = "";
      yearOptions.value = [];
      loading.value = false;
    }
  } catch (error) {
    loading.value = false;
  }
}
// 改变年份
function yearChange(val) {
  selectYear.value = val;
  const res = allData.value.filter((ele) => ele.ssnf == val);
  CardFormData.value = formDataList.value.filter((ele) => ele.year == val)[0];
  tableData.value = res;
}
function handleClick(row) {
  formData.value = {
    lqzdf: row.lqzdf,
    lqzgf: row.lqzgf,
    jyfs: row.jyfs,
    ssnf: row.ssnf,
  };
  editDialog.value = true;
}
const editDialog = ref(false);
function handleClose() {
  editDialog.value = false;
}
const formData = ref({});
const dataScholl = ref({});
async function confirmEdit(type) {
  if (type == 1) {
    dataScholl.value = {
      enrollRsps: [
        {
          lqzdf: formData.value.lqzdf,
          lqzgf: formData.value.lqzgf,
          jyfs: formData.value.jyfs,
          ssnf: formData.value.ssnf,
        },
      ],
      gradSchoolRsp: {
        majorOutRelationId: props.majorId,
      },
    };
  }else if (type == 2) {
    dataScholl.value = {
    enrollRsps: [
      {
        flRatio: CardFormData.value.fuluRaio,
        fsLowScore: CardFormData.value.fuLowScore,
        fsHighScore: CardFormData.value.fuHighScore,
        fsMedianScore: CardFormData.value.fuMedianScore,
        ssnf: CardFormData.value.year,
      },
    ],
    gradSchoolRsp: {
      majorOutRelationId: props.majorId,
    },
  };
  }
  console.log(dataScholl.value);
  const result = await correctMajorInfo(dataScholl.value);
  const deRes = JSON.parse(decrypt(result));
  console.log(deRes);
  if (deRes.status === 0) {
    ElMessage.success("编辑成功!");
    getScoreAnalysis();
    if (type == 1) {
     editDialog.value = !editDialog.value;
    }
  } else {
    ElMessage.warning("编辑失败!");
  }
}
</script>
<style lang="less" scoped>
.card-header {
  font-weight: bold;
}
.score {
  .header {
    text-align: right;
  }
  .con {
    .charts {
      width: 100%;
      height: 320px;
    }
    .text {
      font-size: 18px;
    }
    ::v-deep .el-card__body {
      padding: 0 !important;
    }
  }
}
.mohu {
  position: relative;
  z-index: 9;
}
.table {
  margin-top: 20px;
  ::v-deep .el-card__body {
    padding: 0 !important;
  }
}
.mohu-filter {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 99;
  background-color: rgba(255, 255, 255, 0.6);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
